#include "fig_sfx_manager.h"
#include "fig_sfx_collection.h"

#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros


FIGSFXManager&	FIGSFXManager::Instance()
{
	static FIGSFXManager	Inst;
	return Inst;
}

FIGSFXManager::FIGSFXManager()
{
	// weired bug if i dun write something here
	int i = 0;
	i = 2;
	
}

FIGSFXManager::~FIGSFXManager()
{
	Clear();
	for(unsigned int i = 0; i < m_CacheSfxs.size(); i++)
	{
		m_CacheSfxs[i]->Clear();
		UTIL_SAFE_DELETE(m_CacheSfxs[i]);
	}
	m_CacheSfxs.resize(0);
}

void		FIGSFXManager::RenderAtGround(const math::Vec2& Offset,float Scale)
{
	for(unsigned int i = 0; i < m_SFXCollections.size(); i++)
	{
		FIGSFXCollection* pCol = m_SFXCollections[i];
		pCol->RenderAtGround(Offset,Scale);
	}
}

void		FIGSFXManager::Render(const math::Vec2& Offset,float Scale)
{
	for(unsigned int i = 0; i < m_SFXCollections.size(); i++)
	{
		FIGSFXCollection* pCol = m_SFXCollections[i];
		pCol->Render(Offset,Scale);
	}
}

void		FIGSFXManager::PostRender(const math::Vec2& Offset,float Scale)
{
	for(unsigned int i = 0; i < m_SFXCollections.size(); i++)
	{
		FIGSFXCollection* pCol = m_SFXCollections[i];
		pCol->PostRender(Offset,Scale);
	}

}
	
void		FIGSFXManager::RenderDebug(const math::Vec2& Offset,float Scale)
{
	for(unsigned int i = 0; i < m_SFXCollections.size(); i++)
	{
		FIGSFXCollection* pCol = m_SFXCollections[i];
		pCol->RenderDebug(Offset,Scale);
	}
}


void		FIGSFXManager::Tick(float dt)
{
	for(unsigned int i = 0; i < m_SFXCollections.size(); i++)
	{
		FIGSFXCollection* pCol = m_SFXCollections[i];
		pCol->Tick(dt);
	}
}


void		FIGSFXManager::Clear()
{
	m_CacheSfxs.resize(0);
	for(unsigned int i = 0; i < m_SFXCollections.size(); i++)
	{
		FIGSFXCollection* pCol = m_SFXCollections[i];
		pCol->Clear();
	}

	for( int i = (int)m_SFXCollections.size() - 1; i >= 0; i--)
	{
		FIGSFXCollection* pCol = m_SFXCollections[i];
		UTIL_SAFE_DELETE(pCol);
	}
	m_SFXCollections.resize(0);
}

FIGSfx*		FIGSFXManager::Spawn(FIGSfx::SFX_TYPES Type, 
											const math::Vec2& Pos,
											float SizeScale,
											float Rotation,
											bool	bIsCache,
											FIGSounds::SOUND_NAMES SoundSFXName
											)
{
	FIGSfx* pSFX = NULL;
	bool Found = false;
	for( int i = (int)m_SFXCollections.size() - 1; i >= 0; i--)
	{
		FIGSFXCollection* pCol = m_SFXCollections[i];
		if ( pCol->GetType() == Type)
		{
			pSFX = pCol->Spawn(	Pos, 
								SizeScale, 
								Rotation,
								SoundSFXName
								);
			Found = true;
			i = -1;
		}		
	}

	if(!Found)
	{
		FIGSFXCollection* pCol = new FIGSFXCollection(Type);		
		pSFX = pCol->Spawn(	Pos, 
							SizeScale, 
							Rotation,
							SoundSFXName
							);

		if(!bIsCache)
			m_SFXCollections.push_back(pCol);
	}

	return pSFX;		
}

FIGSfx*		FIGSFXManager::GetCacheSfx(FIGSfx::SFX_TYPES Type)
{
	if((int)m_CacheSfxs.size() < Type + 1)
		return NULL;

	return m_CacheSfxs[(int)Type];
}

void		FIGSFXManager::Cache()
{
	m_CacheSfxs.resize(0);
	for(unsigned int i = 0; i < FIGSfx::SFX_COUNT; i++)
	{
		FIGSfx* pSfx = Spawn((FIGSfx::SFX_TYPES)i, math::Vec2(0.5f,0.5f), 1.0f, 0, true);
		m_CacheSfxs.push_back(pSfx); 	
		pSfx->ToDie();
	}
}

